﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jzy.BLL.Calendar;
using System.Collections.Generic;
using Jzy.Model;

using Jzy.Model.Calendar;
using Frame.Lib.Common;
using Frame.Lib.Core.Dict;
using Frame.Lib.Enums;
using Frame.Lib.Core.Dept;
using Frame.Lib.Utilities;


public partial class Calendar_Edit : PageEdit
{ 
    private CalendarBO cal = new CalendarBO();
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            BindType();
            if (PKID != null)
            {
                int userID = AppSession.UserID;
                int creatorID = CalendarBO.GetCreatorID(int.Parse(PKID));
                if (userID != creatorID)
                {
                    Response.Redirect("Detail.aspx?ID=" + Server.UrlEncode(Request["ID"]) + "&Title=" + Server.UrlEncode("查看日程"), true);
                }
                DispInfo();
            }
            else
            {
                JoinerNames.Text = AppSession.UserName;
                JoinerIDs.Value = "-" + AppSession.UserID;
                btnDelete.Visible = false;
                string strStartTime = Request["StartTime"];
                if (strStartTime != null)
                {
                    StartTime.Value = strStartTime;
                }
                else
                {
                    StartTime.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                }
            }
        }
        AddAttr();
        //AjaxPro.Utility.RegisterTypeForAjax(typeof(Calendar_Edit));

    }

    private void BindType()
    {
        DictBO dict = new DictBO();
        dict.BindListControl(TypeCode, (int)DictItem.日程类型);
       
    }

    private void DispInfo()
    {
        CalendarInfo info = cal.GetInfo(PKID);
        EntityUtil.SetControl(pnlMain, info);
        if (info.EndTime == DateTime.MinValue)
        {
            EndTime.Value = "";
        }
        else
        {
            EndTime.Value = info.EndTime.ToString("yyyy-MM-dd HH:mm:ss");
        }
        if (info.StartTime == DateTime.MinValue)
        {
            StartTime.Value = "";
        }
        else
        {
            StartTime.Value = info.StartTime.ToString("yyyy-MM-dd HH:mm:ss");
        }

       
        JoinerIDs.Value = DeptBO.MergeDeptUser(info.JoinerDeptIDs, info.JoinerUserIDs);      
       
        TypeCode.SelectedValue = info.TypeCode.ToString();
       
    }

    public void AddAttr()
    {
        btnDelete.Attributes.Add("onclick", "return confirm('确认删除');");
       
        JSUtil.ShowDeptUserTreeDlg(JoinerIDs, JoinerNames, false, null, btnBrowse);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {

        if (Name.Text.Trim() == "")
        {
            Alert("请输入标题");
            return;
        }

        if (StartTime.Value.Trim() == "")
        {
            Alert("请输入开始时间");
            return;
        }

        if (JoinerNames.Text.Trim() == "")
        {
            Alert("请选择参与人员");
            return;
        }

        DateTime time = DateTime.Now;
        if (!DateTime.TryParse(StartTime.Value, out time))
        {
            Alert("开始时间格式不对");
            return;
        }
        if (EndTime.Value.Trim() != "")
        {
            if (!DateTime.TryParse(EndTime.Value, out time))
            {
                Alert("结束时间格式不对");
                return;
            }
            else
            {
                if (DateTime.Parse(EndTime.Value) < DateTime.Parse(StartTime.Value))
                {
                    Alert("结束时间不参早于开始时间");
                    return;
                }
            }
        }
            
      


        EndTime.Value = EndTime.Value.Trim();
        CalendarInfo info = cal.GetInfo(PKID);
        EntityUtil.SetEntity(pnlMain, info);

       
        DeptBO.SplitDeptUser(JoinerIDs.Value, info.JoinerDeptIDs, info.JoinerUserIDs);
      
        
        info.CreatorID = AppSession.UserID;
        info.CreatorName = AppSession.UserName;
        info.InputTime = DateTime.Now;
        if (TypeCode.SelectedValue != "")
        {
            info.TypeCode = int.Parse(TypeCode.SelectedValue);
        }


        cal.Save(info);

        JSUtil.RefreshView();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        cal.Delete(PKID);
        JSUtil.RefreshView();
    }

    [AjaxPro.AjaxMethod]
    public bool CheckTime(string strTime)
    {
        try
        {
            DateTime tmp = DateTime.Parse(strTime);
            return true;
        }
        catch (FormatException)
        {
            return false;
        }
    }
}
